+ function($) {
	function Aduioplayer(element, option) {
		this.element = document.querySelector(element)
		this.$rvCounttime = $('.rv-counttime')
		this.$rvProgressbar = $('.rv-progressbar')
		this.$rvPicBox = $('.rv-pic-box')
		this.$rvlItemFlex = $('.rvl-item-flex')
		this.$rvPicWrap = $('.rv-pic-wrap')
		this.$rvPic = $('.rv-pic')
		this.$element = $(this.element)
		this.duration = null
		this.animationTimeId = null
		this.changeAduio()
		this.initAdioSrc()
		this.toggle()
		this.$element.on('play', $.proxy(this.onPlay, this))
		this.$element.on('pause', $.proxy(this.onPause, this))

	}

	Aduioplayer.DEFAULT = {
		
	}

	Aduioplayer.prototype = {

		constuctor: Aduioplayer,

		initAdioSrc: function() {
			this.$rvlItemFlex.eq(0).trigger('click')
		},

		setAduioSrc: function(src) {
			var element = this.element
			element.src = src
		},

		getAduioDuration: function() {
			var element = this.element
			this.duration = element.duration
			return element.duration || 0
		},

		getAduioCurrentTime: function() {
			var element = this.element
			return element.currentTime || 0
		},

		getAduioReadyState: function() {
			var element = this.element
			return element.readyState
		},

		setCounttime: function() {
			var duation = this.getAduioDuration()
			var durationTime = this.formataTime(duation, 0)
			var $rvCounttime = this.$rvCounttime
			$rvCounttime.text(durationTime)
		},

		changeCountTime: function() {
			var currentTime = this.getAduioCurrentTime()
			var duration = this.duration || this.getAduioDuration()
			var $rvCounttime = this.$rvCounttime
			var formataTime = this.formataTime(duration, currentTime)
			$rvCounttime.text(formataTime)
		},

		resetCountTime: function() {
			var $rvCounttime = this.$rvCounttime
			$rvCounttime.text('')
		},

		changeProgress: function() {
			var duration = this.duration
			var currentTime = this.getAduioCurrentTime()
			var percentage = (currentTime / duration) * 100
			var $rvProgress = this.$rvProgressbar
			$rvProgress.css('width', percentage + '%')
		},

		resetProgress: function() {
			var $rvProgress = this.$rvProgressbar
			$rvProgress.width(0)
		},

		changeCP: function() {
			this.changeCountTime()
			this.changeProgress()
			this.audioError()
			this.end()
			this.stalled()
			this.animationTimeId = this.requesetAnimationFrame($.proxy(this.changeCP, this))
		},
		
		animationPlay : function(){
			this.$rvPic.removeClass('pause')
		},
		animationPause : function(){
			this.$rvPic.addClass('pause')
		},
		onPlay: function() {
			this.setCounttime()
			this.changeCP()
			this.$rvPicBox.addClass('rv-play')
			this.$rvPic.addClass('animateRotate').removeClass('initPlay')
			this.animationPlay()
		},
		
		onPause: function() {
			this.animationPause()
			this.$rvPicBox.removeClass('rv-play')
			this.animationTimeId && this.cancelAnimationFrame(this.animationTimeId)
		},
		
		end : function(){
			var element = this.$element
			this.animationTimeId && this.cancelAnimationFrame(this.animationTimeId)
			if(element.ended){
				this.nextAduio()
			}
		},
		stalled : function(){
			var element = this.$element
			this.animationTimeId && this.cancelAnimationFrame(this.animationTimeId)
			if(element.stalled){
				this.nextAduio()
			}
		},
		audioError : function(){
			var element = this.$element
			var errorcode = element.error.code
			this.animationTimeId && this.cancelAnimationFrame(this.animationTimeId)
			switch (errorcode){
				case 1:
					alert('MEDIA_ERR_ABORTED')
					break;
				case 2:
					alert('MEDIA_ERR_NETWORK')
					break;
				case 3:
					alert('MEDIA_ERR_DECODE')
					break;
				default:
					break;
			}
			this.end()
		},
		
		resetAduio: function(src) {
			this.setAduioSrc(src)
			this.$rvPicBox.removeClass('rv-play')
			this.$rvPic.removeClass('animateRotate').addClass('initPlay')
			this.duration = 0;
			this.animationTimeId && self.cancelAnimationFrame(self.animationTimeId)
			this.resetProgress()
			this.resetCountTime()
		},

		changeAduio: function() {
			var self = this;
			$(document).on('click', '.rvl-item-flex', function(e) {
				e.stopPropagation()
				e.preventDefault()
				if($(this).parent().hasClass('active')){
					return
				}
				var src = $(this).data().src
				var imagesrc = $(this).find('.rvl-item-pic').css('background-image')
				$(this).parent().addClass('active played').siblings().removeClass('active')
				self.$rvPic.css('background-image',imagesrc)
				self.resetAduio(src)
				self.$element.trigger('play')
			})
		},
		
		nextAduio : function(){
			var $curAduio = this.$rvPicWrap.find('.active')
			var $nextAduios = $curAduio.nextAll()
			this.resetAduio(src)
			$nextAduios.each(function(i,nextAduio){
				var $nextAduio = $(nextAduio)
				if(!$nextAduio.hasClass('played')){
					$nextAduio.trigger('click')
					return false;
				}
			})
			if(this.$rvPicWrap.find('.active').size() < 1){
				this.$rvlItemFlex.not('.played').eq(0).trigger('click')
			}
		},
		
		toggle: function() {
			var self = this
			this.$rvPicBox.on('click', function(e) {
				e.stopPropagation()
				e.preventDefault()
				var $this = $(this)
				if($this.hasClass('rv-play')) {
					self.$element.trigger('pause')
				} else {
					self.$element.trigger('play')
				}
			})
		},

		formataTime: function(duration, currenttime) {
			var duration = duration
			var currenttime = currenttime
			var countTime = duration - currenttime
			var minuts = Math.floor(countTime / 60);
			var second = parseInt(countTime - minuts * 60)
			if(second < 10) {
				second = '0' + second
			}
			return minuts + ':' + second
		},

		requesetAnimationFrame: function(callback) {
			if(window.requestAnimationFrame) return window.requestAnimationFrame(callback);
			else if(window.webkitRequestAnimationFrame) return window.webkitRequestAnimationFrame(callback);
			else if(window.mozRequestAnimationFrame) return window.mozRequestAnimationFrame(callback);
			else {
				return window.setTimeout(callback, 1000 / 60);
			}
		},

		cancelAnimationFrame: function(id) {
			if(window.cancelAnimationFrame) return window.cancelAnimationFrame(id);
			else if(window.webkitCancelAnimationFrame) return window.webkitCancelAnimationFrame(id);
			else if(window.mozCancelAnimationFrame) return window.mozCancelAnimationFrame(id);
			else {
				return window.clearTimeout(id);
			}
		}
	}

	window['Aduioplayer'] = Aduioplayer

}(jQuery)


